package jzqb.controller;

import haha.life.normal.TokenUtils;
import io.swagger.annotations.Api;
import jzqb.bo.SubscriptBo;
import jzqb.common.Result;
import jzqb.service.SubscriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/subscript")
@Api(tags = "订阅")
public class SubscriptController {

    @Autowired
    SubscriptService subscriptService;

    /**
     * message
     *
     * @return
     */
    @PostMapping("/insertSubscript")
    public Result insertSubscript(@RequestBody SubscriptBo subscriptBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        subscriptBo.setUserId(userId);
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        subscriptBo.setSubscriptTime(now);
        Boolean result = subscriptService.insertSubscript(subscriptBo);
        if (result) {
            return Result.succ("订阅成功");
        } else {
            return Result.succ("已存在相同订阅类型");
        }
    }

    /**
     * 获得某个人的订阅信息
     *
     * @param subscriptBo
     * @param request
     * @return
     */
    @PostMapping("/getAlreadySubscript")
    public Result getAlreadySubscript(@RequestBody SubscriptBo subscriptBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        subscriptBo.setUserId(userId);
        List<SubscriptBo> subscriptBoList = subscriptService.getAlreadySubscript(subscriptBo);
        return Result.succ(subscriptBoList);
    }


    /**
     * 取消此订阅
     *
     * @return
     */
    @PostMapping("/cancelSubscript")
    public Result cancelSubscript(@RequestBody SubscriptBo subscriptBo, HttpServletRequest request) {
        Boolean result = subscriptService.deleteSubscriptById(subscriptBo);
        if (result) {
            return Result.succ("取消订阅成功");
        } else {
            return Result.fail("取消订阅失败");
        }
    }
}
